#!/usr/bin/perl
#si pas le même redpertoire mettre -I a l'appelle de perl suivi du repertoire avec les packages
use strict;
use warnings;
#use File::Basename;
#use File::Spec;
use p_files_with_eval_manager;
package p_index_manager;


sub p_index_manager::create_index {
	#my $directoryName = $_[0];
	#opendir(DIR, $directoryName) || die "Impossible d'ouvrir le dossier dans create_index $directoryName\n";
	#my @files = grep { /.txt$/ } readdir(DIR);
	#closedir DIR;
	
	my @files=@{p_files_with_eval_manager::get_files($_[0])};
	
	my $nbFiles=@files;
#	for my $file (@files) {
#		printf ("$file \n");
#	}
	#	exit;
	
	my ($mot, $cat, $lemme);
	my %index = ();
	my $filename;
	my $nbFilesDone=0;
	print "Files to do : $nbFiles\n";
	for my $file (@files) {
		open (TT, "\$TTDIR/tree-tagger-english $_[0]/$file 2>/dev/null |") || die $!;
		$filename=$file;
		#retirer le .txt de la fin du fichier
		#$filename=basename($file,(".txt"));
		#$filename=~s/[.]txt$//;
		print "$nbFilesDone/$nbFiles : doing $filename\n";
		my $pos=0;
		while (my $ligne = <TT>){
			#print"$ligne\n";
			if($ligne=~ /^(.*)\t(.*)\t(.*)$/){
				$mot=$1; $cat=$2; #$lemme=$3;
				#print"$ligne";
				if($cat eq "POS" && $mot eq "'"){
				        $mot="'s"
				}
				if(($cat ne "PUN" )
				&&($cat ne "SENT")
				&&($cat ne "''")
				&&($mot ne "\"")
				&&($cat ne ",")
				&&($cat ne "(")
				&&($cat ne ")")
				&&($cat ne ":")){
					#if($lemme eq "<unknown>"){
					#	$lemme=$mot;
					#}
					#if($lemme eq "\@card\@"){
					#	$lemme=$mot;
					#}
					#$lemme=lc $lemme;
					my $ref_hDoc;
					if(exists($index{$mot})){
						$ref_hDoc=$index{$mot};
					}else{
						%{$ref_hDoc}=();
						$index{$mot}=$ref_hDoc;
					}
					
					my $ref_hPos;
					#if(exists($ref_hDoc->{$filename})){
					#	$ref_hPos=$ref_hDoc->{$filename};
					#}else{
					#	@{$ref_hPos}=();
					#	$ref_hDoc->{$filename}=$ref_hPos;
					#}
					#push @{$ref_hPos} , $pos;
					if(!exists($ref_hDoc->{$filename})){
						$ref_hDoc->{$filename}=1;
					}else{
						$ref_hDoc->{$filename}++;
					}
				        $pos++;
				}
			}
		}
		close TT;
		$nbFilesDone++;
	};
	return \%index;
}

sub p_index_manager::read_index {
	my $directoryName = $_[0];
	$directoryName=~s/.*[\/](.*)([^\/]*)[\/]$/$1/;
	$directoryName=$1;
	open (FIC_INDEX, "<./f_index_$directoryName.txt") || die "Impossible d'ouvrir le fichier dans read_index $directoryName\n";
	my %index = ();
	my $ref_hDoc;
	my $ref_hPos;
	my $nomDoc;
	while (my $ligne = <FIC_INDEX>){
		if($ligne=~ /^\#lemme\t(.*)$/){
			my %toto = ();
			$ref_hDoc=\%toto;
			$index{$1}=$ref_hDoc;
		}elsif($ligne=~ /^\#doc\t(.*)$/){
			#my @toto = ();
			#$ref_hPos=\@toto;
			$nomDoc=$1;
			$ref_hDoc->{$1}=1;#$ref_hPos;
		}elsif($ligne=~ /^(.*)$/){
			#push @{$ref_hPos} , $1;
			$ref_hDoc->{$nomDoc}=$1;
		}
	}
	close FIC_INDEX;
	return \%index;
}

sub p_index_manager::write_index {
	my %index = %{$_[1]};
	open (OUT, ">./f_index_$_[0].txt") || die $!;
	for my $klemme(sort { $a cmp $b } keys(%index)){
		print OUT "#lemme\t$klemme\n";
		my %hDoc = %{$index{$klemme}};
		for my $kdoc (keys(%hDoc)){
			print OUT "#doc\t$kdoc\n";
			#my @hPos=@{$hDoc{$kdoc}};
			#for my $val (@hPos){
			#	print OUT "$val\n";
			#}
			print OUT "$hDoc{$kdoc}\n";
		}
	}
	close OUT;
}

#get_index(String indexName, boolean regenIndex = 0)
sub p_index_manager::get_index {
	#$directoryName=~s/.*[\/]([^\/]+)$/$1/;
	my $directoryName=$_[0];
	if($directoryName!~ /[\/]$/ ){
		$directoryName="$_[0]/";
	}
	$directoryName=~s/.*[\/](.*)([^\/]*)[\/]$/$1/;
	$directoryName=$1;
	my %index;
        print "Travail sur\t\"$directoryName\"\n";
	if ((!exists($_[1])||$_[1] eq 0)&&-e "./f_index_$directoryName.txt"){
		#print"reading index\n";
		%index=%{&read_index($directoryName)};
	}else{
		print"building index\n";
		my $temps=time();
		if($_[0]=~/(.*)[\/]$/){
			%index=%{&create_index($1)};
		}else{
			%index=%{&create_index($_[0])};
		}
		print"writing index\n";
		print "done in " . (time()-$temps) . " secondes\n";
		&write_index($directoryName,\%index);
	}
	return \%index;
}

1;
#&get_index($ARGV[0],1);
